﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CIT.WMS.RM.Bll
{
    class Bll_Shouhuo
    {
        public static List<string> Bills = new List<string>();

        public static DataTable GetPrintUrl()
        {
            string sqlcmd = string.Format(@"SELECT * FROM [dbo].[wms_S_Option] WHERE OptCode='PrintUrl'");
            var result = Utils.NMS.GetDataTable(sqlcmd);
            return result;
        }

        public static DataTable GetHeadInfo(string condition)
        {
            string sqlcmd = string.Format(@"SELECT * FROM [dbo].[wms_T_InStockRM] WHERE 1=1 {0}", condition);
            var result = Utils.NMS.GetDataTable(sqlcmd);
            return result;
        }

        public static DataTable GetRowInfo(string condition)
        {
            string sqlcmd = string.Format(@"SELECT r.BillNo +','+ CONVERT(varchar(5),RowNo) as CbNo,h.Status as Flg,r.* 
                                            FROM [dbo].[wms_T_InStockRMRows] r inner join wms_T_InstockRM h on r.BillNo=h.BillNo WHERE h.Status=0 {0}", condition);
            var result = Utils.NMS.GetDataTable(sqlcmd);
            return result;
        }

        public static DataTable GetRowLot(string condition)
        {
            string sqlcmd = string.Format(@"SELECT * FROM [dbo].[wms_T_InStockRMRows] WHERE 1=1 {0}", condition);
            var result = Utils.NMS.GetDataTable(sqlcmd);
            return result;
        }

        public static int LockRow(string BillNo, string PartNumber, string Updator)
        {
            string sqlcmd = string.Format(@"UPDATE [dbo].[wms_T_InStockRMRows] SET Updator='{2}',UpdateTime=getdate() WHERE BillNo='{0}' and PartNumber='{1}' and Status=0",
                BillNo, PartNumber, Updator);
            var result = Utils.NMS.ExcuteSQL(sqlcmd);
            return result;
        }

        public static string CheckExpiry(string partNumber, string dateCode)
        {
            string strSql = "select ShelfLifeTime from MdcdatMaterial where MaterialCode='{0}'";
            strSql = string.Format(strSql, partNumber);
            DataTable dt = Utils.NMS.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
                var date = DateTime.ParseExact(dateCode, "yyyyMMdd", format);
                var day = (int)dt.Rows[0][0];

                date = date.AddDays(day);
                if (date <= DateTime.Now.Date)
                {
                    return "输入的生产日期加有效期天数小于当前日期";
                }
            }
            else
            {
                return "物料基础信息未维护";
            }

            return "";
        }

        public static string ExcProc(string billno, string rowno, string userName)
        {
            string sqlcmd = string.Format(@"begin 
                                            DECLARE @ret INT;
                                            exec @ret=WMS_TakeOverNew @billNo=N'{0}', @rowNo=N'{1}',@userName=N'{2}';
                                            SELECT @ret 
                                            end",
                                            billno, rowno, userName);
            var result = Utils.NMS.GetDataTable(sqlcmd);
            return result.Rows[0][0] + ":" + result.Rows[0][1];
        }

        public static int AddLog(string billNo, string rowNo, string userName, string barCode)
        {
            string sqlcmd = string.Format(@"insert into wms_S_PrintLog(PrintDate,BillNo,RowNo,UserName,BarCode,IsPrint) 
values(getdate(),'{0}','{1}','{2}','{3}','0')",
               billNo, rowNo, userName, barCode);
            var result = Utils.NMS.ExcuteSQL(sqlcmd);
            return result;
        }
        //END
    }
}
